{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "from client import get_gvm_conn\n",
    "import json\n",
    "\n",
    "pyg = get_gvm_conn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'owner': {'name': None}, 'name': 'CVE', 'comment': None, 'creation_time': '2024-07-05T13:21:44Z', 'modification_time': '2024-07-05T13:21:44Z', 'writable': '1', 'in_use': '0', 'permissions': {'permission': [{'name': 'get_scanners'}, {'name': 'get_scanners'}, {'name': 'get_scanners'}, {'name': 'get_scanners'}]}, 'host': None, 'port': '0', 'type': '3', 'ca_pub': None, 'credential': {'name': None, 'type': None, 'trash': '0', '@id': ''}, '@id': '6acd0832-df90-11e4-b9d5-28d24461215b'}, {'owner': {'name': None}, 'name': 'OpenVAS Default', 'comment': None, 'creation_time': '2024-07-05T13:21:44Z', 'modification_time': '2024-07-05T13:24:55Z', 'writable': '1', 'in_use': '1', 'permissions': {'permission': [{'name': 'get_scanners'}, {'name': 'get_scanners'}, {'name': 'get_scanners'}, {'name': 'get_scanners'}]}, 'host': '/run/ospd/ospd.sock', 'port': '0', 'type': '2', 'ca_pub': None, 'credential': {'name': None, 'type': None, 'trash': '0', '@id': ''}, '@id': '08b69003-5fc2-4037-a479-93b440211c73'}]\n",
      "CVE\n",
      "6acd0832-df90-11e4-b9d5-28d24461215b\n",
      "OpenVAS Default\n",
      "08b69003-5fc2-4037-a479-93b440211c73\n"
     ]
    }
   ],
   "source": [
    "res = pyg.list_scanners()\n",
    "print(res)\n",
    "print(res[0]['name'])\n",
    "print(res[0]['@id'])\n",
    "print(res[1]['name'])\n",
    "print(res[1]['@id'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'owner': {'name': 'admin'}, 'name': 'a_f1', 'comment': None, 'creation_time': '2024-10-13T03:27:18Z', 'modification_time': '2024-10-13T03:27:18Z', 'writable': '1', 'in_use': '1', 'permissions': {'permission': {'name': 'Everything'}}, 'alterable': '0', 'usage_type': 'scan', 'config': {'name': 'FF2_0', 'trash': '0', '@id': 'b4cdbace-ad43-413c-9e9e-d2b1f5801595'}, 'target': {'name': 'Target for immediate scan of IP 192.168.220.132 - 2024-09-17 12:04:53', 'trash': '0', '@id': '30af06bf-a854-46d8-aa83-537655a10bd3'}, 'hosts_ordering': 'sequential', 'scanner': {'name': 'OpenVAS Default', 'type': '2', 'trash': '0', '@id': '08b69003-5fc2-4037-a479-93b440211c73'}, 'status': 'Running', 'progress': '98', 'report_count': {'finished': '3', '#text': '4'}, 'trend': None, 'schedule': {'name': None, 'trash': '0', '@id': ''}, 'schedule_periods': '0', 'current_report': {'report': {'timestamp': '2024-10-13T08:21:21Z', 'scan_start': '2024-10-13T08:21:37Z', 'scan_end': None, '@id': '7dbfe2c9-7593-4724-b1a7-e4640b63cbb3'}}, 'last_report': {'report': {'timestamp': '2024-10-13T05:55:05Z', 'scan_start': '2024-10-13T05:55:12Z', 'scan_end': '2024-10-13T06:28:14Z', 'result_count': {'hole': '6', 'info': '2', 'log': '67', 'warning': '18', 'false_positive': '0'}, 'severity': '10.0', '@id': '1c51ad7f-a819-4a08-a461-46030d29f769'}}, 'observers': None, 'preferences': {'preference': [{'name': 'Maximum concurrently executed NVTs per host', 'scanner_name': 'max_checks', 'value': '4'}, {'name': 'Maximum concurrently scanned hosts', 'scanner_name': 'max_hosts', 'value': '20'}, {'name': 'Add results to Asset Management', 'scanner_name': 'in_assets', 'value': 'yes'}, {'name': 'Apply Overrides when adding Assets', 'scanner_name': 'assets_apply_overrides', 'value': 'yes'}, {'name': 'Min QOD when adding Assets', 'scanner_name': 'assets_min_qod', 'value': '70'}, {'name': 'Auto Delete Reports', 'scanner_name': 'auto_delete', 'value': 'no'}, {'name': 'Auto Delete Reports Data', 'scanner_name': 'auto_delete_data', 'value': '5'}]}, '@id': 'ddc6e03b-d3b5-4a28-85b9-de1e9f6c1f74'}, {'owner': {'name': 'admin'}, 'name': 'a_f2', 'comment': None, 'creation_time': '2024-10-13T04:45:48Z', 'modification_time': '2024-10-13T04:45:48Z', 'writable': '1', 'in_use': '1', 'permissions': {'permission': {'name': 'Everything'}}, 'alterable': '0', 'usage_type': 'scan', 'config': {'name': 'FF2_1', 'trash': '0', '@id': '11602550-73a8-4bc1-85db-a38c42a1339c'}, 'target': {'name': 'Target for immediate scan of IP 192.168.220.132 - 2024-09-17 12:04:53', 'trash': '0', '@id': '30af06bf-a854-46d8-aa83-537655a10bd3'}, 'hosts_ordering': 'sequential', 'scanner': {'name': 'OpenVAS Default', 'type': '2', 'trash': '0', '@id': '08b69003-5fc2-4037-a479-93b440211c73'}, 'status': 'Running', 'progress': '0', 'report_count': {'finished': '3', '#text': '4'}, 'trend': None, 'schedule': {'name': None, 'trash': '0', '@id': ''}, 'schedule_periods': '0', 'current_report': {'report': {'timestamp': '2024-10-13T08:49:47Z', 'scan_start': '2024-10-13T08:49:58Z', 'scan_end': None, '@id': 'c2d730aa-eeaa-42fc-8fa4-52ff138d6e24'}}, 'last_report': {'report': {'timestamp': '2024-10-13T08:21:20Z', 'scan_start': '2024-10-13T08:21:37Z', 'scan_end': '2024-10-13T08:46:20Z', 'result_count': {'hole': '17', 'info': '4', 'log': '78', 'warning': '23', 'false_positive': '0'}, 'severity': '10.0', '@id': '4ca72699-f116-4349-bf24-0a1f6cd6dbc0'}}, 'observers': None, 'preferences': {'preference': [{'name': 'Maximum concurrently executed NVTs per host', 'scanner_name': 'max_checks', 'value': '4'}, {'name': 'Maximum concurrently scanned hosts', 'scanner_name': 'max_hosts', 'value': '20'}, {'name': 'Add results to Asset Management', 'scanner_name': 'in_assets', 'value': 'yes'}, {'name': 'Apply Overrides when adding Assets', 'scanner_name': 'assets_apply_overrides', 'value': 'yes'}, {'name': 'Min QOD when adding Assets', 'scanner_name': 'assets_min_qod', 'value': '70'}, {'name': 'Auto Delete Reports', 'scanner_name': 'auto_delete', 'value': 'no'}, {'name': 'Auto Delete Reports Data', 'scanner_name': 'auto_delete_data', 'value': '5'}]}, '@id': '470d21c1-76b8-44bd-bc75-41002592bab0'}]\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "res = pyg.list_tasks(status=\"Running\")\n",
    "print(res)\n",
    "print(len(res.data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'owner': {'name': 'admin'}, 'name': 'test_1', 'comment': None, 'creation_time': '2024-09-27T07:30:47Z', 'modification_time': '2024-09-27T07:30:47Z', 'writable': '1', 'in_use': '0', 'permissions': {'permission': {'name': 'Everything'}}, 'hosts': '127.0.0.1', 'exclude_hosts': None, 'max_hosts': '1', 'port_list': {'name': 'All IANA assigned TCP', 'trash': '0', '@id': '33d0cd82-57c6-11e1-8ed1-406186ea4fc5'}, 'ssh_credential': {'name': None, 'port': None, 'trash': '0', '@id': ''}, 'smb_credential': {'name': None, 'trash': '0', '@id': ''}, 'esxi_credential': {'name': None, 'trash': '0', '@id': ''}, 'snmp_credential': {'name': None, 'trash': '0', '@id': ''}, 'ssh_elevate_credential': {'name': None, 'trash': '0', '@id': ''}, 'reverse_lookup_only': '0', 'reverse_lookup_unify': '0', 'alive_tests': 'Scan Config Default', 'allow_simultaneous_ips': '1', '@id': '5582420f-725d-4895-9fed-891c6c6d8062'}\n"
     ]
    }
   ],
   "source": [
    "# try:\n",
    "#     pyg.create_target(name=\"test_1\", hosts=[\"127.0.0.1\"])\n",
    "# except Exception as e:\n",
    "#     if \"exists already\" in str(e):\n",
    "#         raise\n",
    "\n",
    "targets = pyg.list_targets(kwargs={'name':'test_1'})\n",
    "print(targets[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "owner\n",
      "name\n",
      "comment\n",
      "creation_time\n",
      "modification_time\n",
      "writable\n",
      "in_use\n",
      "permissions\n",
      "family_count\n",
      "nvt_count\n",
      "type\n",
      "usage_type\n",
      "predefined\n",
      "families\n",
      "max_nvt_count\n",
      "known_nvt_count\n",
      "preferences\n",
      "nvt_selectors\n",
      "@id\n",
      "True\n",
      "family\n",
      "True\n",
      "58\n",
      "name\n",
      "nvt_count\n",
      "max_nvt_count\n",
      "growing\n",
      "Amazon Linux Local Security Checks\n",
      "748\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "config = pyg.get_config(\"daba56c8-73ec-11df-a475-002264764cea\")\n",
    "# print(json.dumps(config.data, indent=2))\n",
    "# print( isinstance(config.data, dict))\n",
    "# print(len(config.data))\n",
    "\n",
    "for k in config.data.keys():\n",
    "    print(k)\n",
    "\n",
    "print( isinstance(config.data[\"families\"], dict))\n",
    "for k in config.data[\"families\"].keys():\n",
    "    print(k)\n",
    "print( isinstance(config.data[\"families\"][\"family\"], list))\n",
    "print( len(config.data[\"families\"][\"family\"]))\n",
    "for k in config.data[\"families\"][\"family\"][1].keys():\n",
    "    print(k)\n",
    "\n",
    "print(config.data[\"families\"][\"family\"][1][\"name\"])\n",
    "print(config.data[\"families\"][\"family\"][1][\"nvt_count\"])\n",
    "print(config.data[\"families\"][\"family\"][1][\"growing\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "resp = pyg.create_config(\"FF1_1\", \"daba56c8-73ec-11df-a475-002264764cea\")\n",
    "print(resp.data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "58\n",
      "{'name': 'AIX Local Security Checks', 'nvt_count': '1', 'max_nvt_count': '1', 'growing': '1'}\n",
      "{'name': 'Amazon Linux Local Security Checks', 'nvt_count': '748', 'max_nvt_count': '748', 'growing': '1'}\n",
      "{'name': 'Brute force attacks', 'nvt_count': '9', 'max_nvt_count': '9', 'growing': '1'}\n",
      "{'name': 'Buffer overflow', 'nvt_count': '650', 'max_nvt_count': '650', 'growing': '1'}\n",
      "{'name': 'CISCO', 'nvt_count': '651', 'max_nvt_count': '651', 'growing': '1'}\n",
      "{'name': 'CentOS Local Security Checks', 'nvt_count': '3255', 'max_nvt_count': '3255', 'growing': '1'}\n",
      "{'name': 'Citrix Xenserver Local Security Checks', 'nvt_count': '30', 'max_nvt_count': '30', 'growing': '1'}\n",
      "{'name': 'Compliance', 'nvt_count': '15', 'max_nvt_count': '15', 'growing': '1'}\n",
      "{'name': 'Databases', 'nvt_count': '1096', 'max_nvt_count': '1096', 'growing': '1'}\n",
      "{'name': 'Debian Local Security Checks', 'nvt_count': '17080', 'max_nvt_count': '17080', 'growing': '1'}\n",
      "{'name': 'Default Accounts', 'nvt_count': '302', 'max_nvt_count': '302', 'growing': '1'}\n",
      "{'name': 'Denial of Service', 'nvt_count': '2237', 'max_nvt_count': '2237', 'growing': '1'}\n",
      "{'name': 'F5 Local Security Checks', 'nvt_count': '125', 'max_nvt_count': '125', 'growing': '1'}\n",
      "{'name': 'FTP', 'nvt_count': '174', 'max_nvt_count': '174', 'growing': '1'}\n",
      "{'name': 'Fedora Local Security Checks', 'nvt_count': '26902', 'max_nvt_count': '26902', 'growing': '1'}\n",
      "{'name': 'FortiOS Local Security Checks', 'nvt_count': '34', 'max_nvt_count': '34', 'growing': '1'}\n",
      "{'name': 'FreeBSD Local Security Checks', 'nvt_count': '2009', 'max_nvt_count': '2009', 'growing': '1'}\n",
      "{'name': 'Gain a shell remotely', 'nvt_count': '109', 'max_nvt_count': '109', 'growing': '1'}\n",
      "{'name': 'General', 'nvt_count': '7915', 'max_nvt_count': '7916', 'growing': '1'}\n",
      "{'name': 'Gentoo Local Security Checks', 'nvt_count': '2191', 'max_nvt_count': '2191', 'growing': '1'}\n",
      "{'name': 'HP-UX Local Security Checks', 'nvt_count': '15', 'max_nvt_count': '15', 'growing': '1'}\n",
      "{'name': 'Huawei', 'nvt_count': '146', 'max_nvt_count': '146', 'growing': '1'}\n",
      "{'name': 'Huawei EulerOS Local Security Checks', 'nvt_count': '12006', 'max_nvt_count': '12006', 'growing': '1'}\n",
      "{'name': 'IT-Grundschutz', 'nvt_count': '195', 'max_nvt_count': '195', 'growing': '1'}\n",
      "{'name': 'IT-Grundschutz-15', 'nvt_count': '85', 'max_nvt_count': '85', 'growing': '1'}\n",
      "{'name': 'IT-Grundschutz-deprecated', 'nvt_count': '363', 'max_nvt_count': '363', 'growing': '1'}\n",
      "{'name': 'JunOS Local Security Checks', 'nvt_count': '136', 'max_nvt_count': '136', 'growing': '1'}\n",
      "{'name': 'Mac OS X Local Security Checks', 'nvt_count': '400', 'max_nvt_count': '400', 'growing': '1'}\n",
      "{'name': 'Mageia Linux Local Security Checks', 'nvt_count': '5342', 'max_nvt_count': '5342', 'growing': '1'}\n",
      "{'name': 'Malware', 'nvt_count': '58', 'max_nvt_count': '58', 'growing': '1'}\n",
      "{'name': 'Mandrake Local Security Checks', 'nvt_count': '807', 'max_nvt_count': '807', 'growing': '1'}\n",
      "{'name': 'Nmap NSE', 'nvt_count': '154', 'max_nvt_count': '154', 'growing': '1'}\n",
      "{'name': 'Nmap NSE net', 'nvt_count': '177', 'max_nvt_count': '177', 'growing': '1'}\n",
      "{'name': 'Oracle Linux Local Security Checks', 'nvt_count': '1895', 'max_nvt_count': '1895', 'growing': '1'}\n",
      "{'name': 'Palo Alto PAN-OS Local Security Checks', 'nvt_count': '40', 'max_nvt_count': '40', 'growing': '1'}\n",
      "{'name': 'Peer-To-Peer File Sharing', 'nvt_count': '9', 'max_nvt_count': '9', 'growing': '1'}\n",
      "{'name': 'Policy', 'nvt_count': '740', 'max_nvt_count': '740', 'growing': '1'}\n",
      "{'name': 'Port scanners', 'nvt_count': '2', 'max_nvt_count': '9', 'growing': '0'}\n",
      "{'name': 'Privilege escalation', 'nvt_count': '161', 'max_nvt_count': '161', 'growing': '1'}\n",
      "{'name': 'Product detection', 'nvt_count': '2974', 'max_nvt_count': '2974', 'growing': '1'}\n",
      "{'name': 'RPC', 'nvt_count': '4', 'max_nvt_count': '4', 'growing': '1'}\n",
      "{'name': 'Red Hat Local Security Checks', 'nvt_count': '1853', 'max_nvt_count': '1853', 'growing': '1'}\n",
      "{'name': 'Remote file access', 'nvt_count': '56', 'max_nvt_count': '56', 'growing': '1'}\n",
      "{'name': 'SMTP problems', 'nvt_count': '53', 'max_nvt_count': '53', 'growing': '1'}\n",
      "{'name': 'SNMP', 'nvt_count': '12', 'max_nvt_count': '12', 'growing': '1'}\n",
      "{'name': 'SSL and TLS', 'nvt_count': '86', 'max_nvt_count': '86', 'growing': '1'}\n",
      "{'name': 'Service detection', 'nvt_count': '247', 'max_nvt_count': '248', 'growing': '1'}\n",
      "{'name': 'Settings', 'nvt_count': '11', 'max_nvt_count': '11', 'growing': '1'}\n",
      "{'name': 'Slackware Local Security Checks', 'nvt_count': '1590', 'max_nvt_count': '1590', 'growing': '1'}\n",
      "{'name': 'Solaris Local Security Checks', 'nvt_count': '1', 'max_nvt_count': '1', 'growing': '1'}\n",
      "{'name': 'SuSE Local Security Checks', 'nvt_count': '21225', 'max_nvt_count': '21225', 'growing': '1'}\n",
      "{'name': 'Ubuntu Local Security Checks', 'nvt_count': '14069', 'max_nvt_count': '14069', 'growing': '1'}\n",
      "{'name': 'Useless services', 'nvt_count': '16', 'max_nvt_count': '16', 'growing': '1'}\n",
      "{'name': 'VMware Local Security Checks', 'nvt_count': '60', 'max_nvt_count': '60', 'growing': '1'}\n",
      "{'name': 'Web Servers', 'nvt_count': '932', 'max_nvt_count': '932', 'growing': '1'}\n",
      "{'name': 'Web application abuses', 'nvt_count': '9244', 'max_nvt_count': '9244', 'growing': '1'}\n",
      "{'name': 'Windows', 'nvt_count': '277', 'max_nvt_count': '277', 'growing': '1'}\n",
      "{'name': 'Windows : Microsoft Bulletins', 'nvt_count': '3385', 'max_nvt_count': '3385', 'growing': '1'}\n"
     ]
    }
   ],
   "source": [
    "resp = pyg.get_config(\"daba56c8-73ec-11df-a475-002264764cea\")\n",
    "print(len(resp.data[\"families\"][\"family\"]))\n",
    "for family in resp.data[\"families\"][\"family\"]:\n",
    "    print(family)\n",
    "# print(json.dumps(resp.data, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "family = \"Amazon Linux Local Security Checks\"\n",
    "config_str = \"daba56c8-73ec-11df-a475-002264764cea\"\n",
    "res = pyg.list_config_nvts(details=False, config_id=config_str, family=family)\n",
    "print(json.dumps(res.data, indent=2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 拷贝Fast And Full 配置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 首先尝试获取划分好的配置\n",
    "# 针对一个扫描器\n",
    "\n",
    "from config import logger\n",
    "from pygvm.pygvm import Pygvm\n",
    "from pygvm.exceptions import HTTPError\n",
    "from config import settings\n",
    "import requests\n",
    "from sqlctrl import insert_splite_task, get_splite_task\n",
    "\n",
    "def create_ff_config(pyg: Pygvm, ffid:str, splite_num:int):\n",
    "    config_ids = {}\n",
    "    whole_families = {}\n",
    "    config_num = {} \n",
    "    config_family = {}\n",
    "    for num in range(splite_num):\n",
    "        config_name = \"FF\"+str(splite_num)+\"_\"+str(num)\n",
    "        resp = pyg.create_config(config_name, ffid, \"Created by scan_server\")\n",
    "        # 调整配置\n",
    "        config_id = resp.data[\"@id\"]\n",
    "        config_num[config_id] = 0\n",
    "        config_family[config_id] = []\n",
    "        resp = pyg.get_config(config_id)\n",
    "        family_list = resp.data[\"families\"][\"family\"]\n",
    "        for family in family_list:\n",
    "            family_name = family[\"name\"]\n",
    "            if family_name in whole_families:\n",
    "                continue\n",
    "            nvts = pyg.list_config_nvts(details=False, config_id=config_id, family=family_name)\n",
    "            # 获取所有oid\n",
    "            oid_list = []\n",
    "            for nvt in nvts:\n",
    "                oid_list.append(nvt[\"@oid\"])\n",
    "            # 获取num~num+1/splite_num区间oid\n",
    "            all_nvt_num = len(oid_list) \n",
    "            nvt_num = len(oid_list) // splite_num\n",
    "            if num == splite_num-1:\n",
    "                oid_list = oid_list[num*nvt_num:]\n",
    "            else:\n",
    "                oid_list = oid_list[num*nvt_num: (num+1)*nvt_num]\n",
    "            # 设置\n",
    "            try:\n",
    "                pyg.modify_config_nvt(config_id=config_id, family=family_name, nvt_oids=oid_list)\n",
    "                config_family[config_id].append(family_name)\n",
    "            except HTTPError as e:\n",
    "                if \"whole-only\" in str(e):\n",
    "                    whole_families[family_name] = all_nvt_num\n",
    "                else:\n",
    "                    raise\n",
    "        config_ids[config_name] = config_id\n",
    "    # 调整whole-family部分\n",
    "    # print(whole_families)\n",
    "    \n",
    "    sorted_families = sorted(whole_families.items(),key = lambda x:x[1],reverse = True)\n",
    "    for family_name, all_nvt_num in sorted_families:\n",
    "        min_config = None\n",
    "        min_num = 1000000000\n",
    "        for config, num in config_num.items():\n",
    "            if min_num > num:\n",
    "                min_config = config\n",
    "                min_num = num\n",
    "        if min_config is not None:\n",
    "            config_family[min_config].append(family_name)\n",
    "            config_num[min_config] = num + all_nvt_num       \n",
    "    # print(config_family)\n",
    "    # return []\n",
    "    for config_id, families in config_family.items():\n",
    "        families_tuple = []\n",
    "        for family_name in families:\n",
    "            if family_name in whole_families:\n",
    "                families_tuple.append((family_name, True, True))\n",
    "            else:\n",
    "                families_tuple.append((family_name, True, False))\n",
    "        pyg.modify_config_family(config_id=config_id, families=families_tuple)    \n",
    "    \n",
    "    return config_ids\n",
    "\n",
    "\n",
    "def check_splited_configs(pyg: Pygvm, splite_num: int):\n",
    "    configs = pyg.list_configs({\"rows\":100})\n",
    "    config_name = \"FF\" + str(splite_num)\n",
    "    config_ids = {}\n",
    "    ffid = None\n",
    "    for config in configs:\n",
    "        # 创建以Full and fast为蓝本\n",
    "        if config[\"name\"] == \"Full and fast\":\n",
    "            ffid = config[\"@id\"]            \n",
    "        if config_name in config[\"name\"]:\n",
    "            config_ids[config[\"name\"]] = config[\"@id\"]\n",
    "\n",
    "    if len(config_ids.keys()) != splite_num:\n",
    "        # 全部删除重新创建\n",
    "        for config_id in config_ids.values():\n",
    "            try:\n",
    "                pyg.delete_config(config_id)\n",
    "            except Exception as e:\n",
    "                logger.error(\"Delete config error: %s\", str(e))\n",
    "                return False, \"DeleteConfigFailed\", None\n",
    "        config_ids = {}\n",
    "    \n",
    "    if len(config_ids.keys()) == 0:\n",
    "        try:\n",
    "            # print(ffid)\n",
    "            config_ids = create_ff_config(pyg=pyg, ffid=ffid, splite_num=splite_num)        \n",
    "        except Exception as e:\n",
    "            logger.error(\"Create config error: %s\", str(e))\n",
    "            print(e.with_traceback)\n",
    "            return False, \"CreateConfigFailed\", None\n",
    "    \n",
    "    if len(config_ids.keys()) != splite_num:\n",
    "        return False, \"ConfigNumError\", None\n",
    "    \n",
    "    return True, None, config_ids\n",
    "\n",
    "\n",
    "# 分配给各个扫描器，并记录相关数据到数据库\n",
    "def map_to_scanners(splite_num:int, target:str, id: str):\n",
    "    scanner_list = settings.scanner_list\n",
    "    scanner_task_list = []\n",
    "    for scanner_host in scanner_list:\n",
    "        url = f\"http://{scanner_host}/gvm/get_task_num\"\n",
    "        try:\n",
    "            resp = requests.get(\n",
    "                url,\n",
    "                headers={'secret-key':settings.secret_key}\n",
    "            )\n",
    "            resp.raise_for_status()\n",
    "            data = resp.json()\n",
    "            if not data['ok']:\n",
    "                logger.error(f\"Get task num from gvm {scanner_host} failed, {data['errmsg']}\")\n",
    "            task_num = data['task_num']\n",
    "            scanner_task_list.append((scanner_host, task_num))\n",
    "        except Exception as e:\n",
    "            logger.error(f\"Get task num from gvm {scanner_host} failed, {str(e)}\")\n",
    "            continue\n",
    "    scanner_task_list = sorted(scanner_task_list, key=lambda x:x[1], reverse=False)\n",
    "    if len(scanner_task_list) == 0:\n",
    "        return False, \"NoScannerAvailable\"\n",
    "    \n",
    "    max_task_num = settings.max_task_num\n",
    "    creatable_task_num = 0\n",
    "    for scanner_task in scanner_task_list:\n",
    "        creatable_task_num += max_task_num - scanner_task[1]\n",
    "    if creatable_task_num < splite_num:\n",
    "        return False, \"NoEnoughScanner\"\n",
    "    \n",
    "    # 分发下去\n",
    "    for num in range(splite_num):\n",
    "        while(True):\n",
    "            scanner_num = len(scanner_task_list)\n",
    "            if scanner_num == 0:\n",
    "                return False, \"NoScannerAvailable\"\n",
    "            idx = scanner_num-1\n",
    "            for i in range(scanner_num):\n",
    "                if i == scanner_num-1:\n",
    "                    break\n",
    "                elif scanner_task_list[i][1] < scanner_task_list[i+1][1]:\n",
    "                    idx = i\n",
    "                    break\n",
    "                else:\n",
    "                    continue    \n",
    "            scanner_host = scanner_task_list[idx][0]\n",
    "            try:\n",
    "                url = f\"http://{scanner_host}/gvm/create_task_with_config\"\n",
    "                resp = requests.post(\n",
    "                    url,\n",
    "                    headers={'secret-key':settings.secret_key},\n",
    "                    params={'target':target, 'id':id, 'num':num, 'splite_num':splite_num}\n",
    "                )\n",
    "                resp.raise_for_status()\n",
    "                data = resp.json()\n",
    "                if not data['ok']:\n",
    "                    logger.error(f\"Map task num to gvm {scanner_host} failed, {data['errmsg']}\")\n",
    "                    scanner_task_list.pop(idx)\n",
    "                # 存起来到数据库中\n",
    "                task_id = data['running_id']\n",
    "                ok = insert_splite_task(id=id, task_id=task_id, scanner=scanner_host)\n",
    "                if not ok:\n",
    "                    logger.error(f\"Map task num to gvm {scanner_host} failed, insert into tasks error\")\n",
    "                    return False, \"InsertTasksError\"\n",
    "                scanner_task_list[i][1] += 1\n",
    "                break\n",
    "            except Exception as e:\n",
    "                logger.error(f\"Get task num from gvm {scanner_host} failed, {str(e)}\")\n",
    "                scanner_task_list.pop(idx)\n",
    "                continue\n",
    "\n",
    "\n",
    "def get_splite_task_status(id: str):\n",
    "    tasks = get_splite_task(id=id)\n",
    "    # 获取每一个task的状态\n",
    "    unfinished_num = 0\n",
    "    for task in tasks:\n",
    "        scanner_host = task[0]\n",
    "        task_id = task[1]\n",
    "        try:\n",
    "            url = f\"http://{scanner_host}/gvm/get_task\"\n",
    "            resp = requests.post(\n",
    "                url,\n",
    "                headers={'secret-key':settings.secret_key},\n",
    "                params={'running_id':task_id}\n",
    "            )\n",
    "            data = resp.json()\n",
    "            if not data['ok']:\n",
    "                logger.error(f\"Reduce task from gvm {scanner_host} failed, {data['errmsg']}\")\n",
    "                return False, \"ReduceGetTaskError\"\n",
    "            status = data[\"running_status\"]\n",
    "            if status != \"Done\":\n",
    "                unfinished_num += 1\n",
    "        except Exception as e:\n",
    "            logger.error(f\"Reduce task from gvm {scanner_host} failed, {str(e)}\")\n",
    "            return False, \"ReduceGetTaskError\"\n",
    "    if unfinished_num != 0:\n",
    "        return False, None\n",
    "    return True, None\n",
    "                       \n",
    "\n",
    "def reduce_splite_task(id: str):\n",
    "    tasks = get_splite_task(id=id)\n",
    "    results = []\n",
    "    hase = []\n",
    "    for task in tasks:\n",
    "        scanner_host = task[0]\n",
    "        task_id = task[1]\n",
    "        try:\n",
    "            url = f\"http://{scanner_host}/gvm/get_task_result\"\n",
    "            resp = requests.post(\n",
    "                url,\n",
    "                headers={'secret-key':settings.secret_key},\n",
    "                params={'running_id':task_id}\n",
    "            )\n",
    "            data = resp.json()\n",
    "            if not data['ok']:\n",
    "                logger.error(f\"Get task result from gvm {scanner_host} failed, {data['errmsg']}\")\n",
    "                return False, \"GetTaskResultError\"\n",
    "            sub_results = resp[\"vuls\"]\n",
    "            for sub_result in sub_results:\n",
    "                if \"nvt\" in sub_result and \"@oid\" in sub_result[\"nvt\"]:\n",
    "                    oid = sub_result[\"nvt\"][\"@oid\"]\n",
    "                else:\n",
    "                    oid = sub_result[\"name\"]\n",
    "                if oid in hase:\n",
    "                    continue\n",
    "                hase.append(oid)\n",
    "                results.append(sub_result)\n",
    "        except Exception as e:\n",
    "            logger.error(f\"Get task result from gvm {scanner_host} failed, {str(e)}\")\n",
    "            return False, \"GetTaskResultError\"\n",
    "    return True, results              \n",
    "\n",
    "def delete_splite_task(id: str):\n",
    "    tasks = get_splite_task(id=id)\n",
    "    for task in tasks:\n",
    "        scanner_host = task[0]\n",
    "        task_id = task[1]\n",
    "        try:\n",
    "            url = f\"http://{scanner_host}/gvm/delete_task\"\n",
    "            resp = requests.post(\n",
    "                url,\n",
    "                headers={'secret-key':settings.secret_key},\n",
    "                params={'running_id':task_id}\n",
    "            )\n",
    "            data = resp.json()\n",
    "            if not data['ok']:\n",
    "                logger.error(f\"Delete task from gvm {scanner_host} failed, {data['errmsg']}\")\n",
    "                return False, \"DeleteTaskError\"\n",
    "        except Exception as e:\n",
    "            logger.error(f\"Delete task from gvm {scanner_host} failed, {str(e)}\")\n",
    "            return False, \"DeleteTaskError\"\n",
    "    return True, None          \n",
    "\n",
    "# ok, msg, configs = check_splited_configs(pyg, 3)\n",
    "# print(ok)\n",
    "# print(configs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "daba56c8-73ec-11df-a475-002264764cea\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "BaseException.with_traceback() takes exactly one argument (0 given)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mGvmError\u001b[0m                                  Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[25], line 104\u001b[0m, in \u001b[0;36mcheck_splited_configs\u001b[0;34m(pyg, splite_num)\u001b[0m\n\u001b[1;32m    103\u001b[0m     \u001b[38;5;28mprint\u001b[39m(ffid)\n\u001b[0;32m--> 104\u001b[0m     config_ids \u001b[38;5;241m=\u001b[39m \u001b[43mcreate_ff_config\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpyg\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpyg\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mffid\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mffid\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msplite_num\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msplite_num\u001b[49m\u001b[43m)\u001b[49m        \n\u001b[1;32m    105\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n",
      "Cell \u001b[0;32mIn[25], line 29\u001b[0m, in \u001b[0;36mcreate_ff_config\u001b[0;34m(pyg, ffid, splite_num)\u001b[0m\n\u001b[1;32m     28\u001b[0m     \u001b[38;5;28;01mcontinue\u001b[39;00m\n\u001b[0;32m---> 29\u001b[0m nvts \u001b[38;5;241m=\u001b[39m \u001b[43mpyg\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mlist_config_nvts\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdetails\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig_id\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mconfig_id\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfamily\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfamily_name\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     30\u001b[0m \u001b[38;5;66;03m# 获取所有oid\u001b[39;00m\n",
      "File \u001b[0;32m/home/gunicorn/scan_server/pygvm/pygvm.py:105\u001b[0m, in \u001b[0;36mPygvm.list_config_nvts\u001b[0;34m(self, details, config_id, family)\u001b[0m\n\u001b[1;32m    104\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mlist_config_nvts\u001b[39m(\u001b[38;5;28mself\u001b[39m, details:\u001b[38;5;28mbool\u001b[39m\u001b[38;5;241m|\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, config_id:\u001b[38;5;28mstr\u001b[39m\u001b[38;5;241m|\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, family:\u001b[38;5;28mstr\u001b[39m\u001b[38;5;241m|\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m):\n\u001b[0;32m--> 105\u001b[0m     resp \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgmp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_scan_config_nvts\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdetails\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdetails\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig_id\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mconfig_id\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfamily\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfamily\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    106\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_list(resp\u001b[38;5;241m=\u001b[39mresp, data_type\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mnvt\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
      "File \u001b[0;32m/usr/local/lib/python3.11/dist-packages/gvm/protocols/gmp/_gmp224.py:3270\u001b[0m, in \u001b[0;36mGMPv224.get_scan_config_nvts\u001b[0;34m(self, details, preferences, preference_count, timeout, config_id, preferences_config_id, family, sort_order, sort_field)\u001b[0m\n\u001b[1;32m   3256\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Request a list of nvts\u001b[39;00m\n\u001b[1;32m   3257\u001b[0m \n\u001b[1;32m   3258\u001b[0m \u001b[38;5;124;03mArgs:\u001b[39;00m\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m   3268\u001b[0m \u001b[38;5;124;03m    sort_field: Sort field\u001b[39;00m\n\u001b[1;32m   3269\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m-> 3270\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_send_and_transform_command\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m   3271\u001b[0m \u001b[43m    \u001b[49m\u001b[43mNvts\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_scan_config_nvts\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m   3272\u001b[0m \u001b[43m        \u001b[49m\u001b[43mdetails\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdetails\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   3273\u001b[0m \u001b[43m        \u001b[49m\u001b[43mpreferences\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpreferences\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   3274\u001b[0m \u001b[43m        \u001b[49m\u001b[43mpreference_count\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpreference_count\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   3275\u001b[0m \u001b[43m        \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtimeout\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   3276\u001b[0m \u001b[43m        \u001b[49m\u001b[43mconfig_id\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mconfig_id\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   3277\u001b[0m \u001b[43m        \u001b[49m\u001b[43mpreferences_config_id\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpreferences_config_id\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   3278\u001b[0m \u001b[43m        \u001b[49m\u001b[43mfamily\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfamily\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   3279\u001b[0m \u001b[43m        \u001b[49m\u001b[43msort_order\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msort_order\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   3280\u001b[0m \u001b[43m        \u001b[49m\u001b[43msort_field\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msort_field\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   3281\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   3282\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m/usr/local/lib/python3.11/dist-packages/gvm/protocols/_protocol.py:137\u001b[0m, in \u001b[0;36mGvmProtocol._send_and_transform_command\u001b[0;34m(self, cmd)\u001b[0m\n\u001b[1;32m    136\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_send_and_transform_command\u001b[39m(\u001b[38;5;28mself\u001b[39m, cmd: Request) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m T:\n\u001b[0;32m--> 137\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_transform(\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_send_command\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcmd\u001b[49m\u001b[43m)\u001b[49m)\n",
      "File \u001b[0;32m/usr/local/lib/python3.11/dist-packages/gvm/protocols/_protocol.py:134\u001b[0m, in \u001b[0;36mGvmProtocol._send_command\u001b[0;34m(self, cmd)\u001b[0m\n\u001b[1;32m    133\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdisconnect()\n\u001b[0;32m--> 134\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m e\n",
      "File \u001b[0;32m/usr/local/lib/python3.11/dist-packages/gvm/protocols/_protocol.py:129\u001b[0m, in \u001b[0;36mGvmProtocol._send_command\u001b[0;34m(self, cmd)\u001b[0m\n\u001b[1;32m    128\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m response:\n\u001b[0;32m--> 129\u001b[0m     received_data \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_read\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    130\u001b[0m     response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_protocol\u001b[38;5;241m.\u001b[39mreceive_data(received_data)\n",
      "File \u001b[0;32m/usr/local/lib/python3.11/dist-packages/gvm/protocols/_protocol.py:66\u001b[0m, in \u001b[0;36mGvmProtocol._read\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m     61\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Read a command response from gvmd\u001b[39;00m\n\u001b[1;32m     62\u001b[0m \n\u001b[1;32m     63\u001b[0m \u001b[38;5;124;03mReturns:\u001b[39;00m\n\u001b[1;32m     64\u001b[0m \u001b[38;5;124;03m    str: Response from server.\u001b[39;00m\n\u001b[1;32m     65\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m---> 66\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_connection\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mread\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m/usr/local/lib/python3.11/dist-packages/gvm/connections/_connection.py:105\u001b[0m, in \u001b[0;36mAbstractGvmConnection.read\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    103\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m data:\n\u001b[1;32m    104\u001b[0m     \u001b[38;5;66;03m# Connection was closed by server\u001b[39;00m\n\u001b[0;32m--> 105\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m GvmError(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mRemote closed the connection\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m    107\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m break_timeout \u001b[38;5;129;01mand\u001b[39;00m time() \u001b[38;5;241m>\u001b[39m break_timeout:\n",
      "\u001b[0;31mGvmError\u001b[0m: Remote closed the connection",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[26], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m ok, msg, configs \u001b[38;5;241m=\u001b[39m \u001b[43mcheck_splited_configs\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpyg\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m      2\u001b[0m \u001b[38;5;28mprint\u001b[39m(ok)\n\u001b[1;32m      3\u001b[0m \u001b[38;5;28mprint\u001b[39m(msg)\n",
      "Cell \u001b[0;32mIn[25], line 107\u001b[0m, in \u001b[0;36mcheck_splited_configs\u001b[0;34m(pyg, splite_num)\u001b[0m\n\u001b[1;32m    105\u001b[0m     \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m    106\u001b[0m         logger\u001b[38;5;241m.\u001b[39merror(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCreate config error: \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28mstr\u001b[39m(e))\n\u001b[0;32m--> 107\u001b[0m         \u001b[38;5;28mprint\u001b[39m(\u001b[43me\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mwith_traceback\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m)\n\u001b[1;32m    108\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCreateConfigFailed\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m    110\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(config_ids\u001b[38;5;241m.\u001b[39mkeys()) \u001b[38;5;241m!=\u001b[39m splite_num:\n",
      "\u001b[0;31mTypeError\u001b[0m: BaseException.with_traceback() takes exactly one argument (0 given)"
     ]
    }
   ],
   "source": [
    "ok, msg, configs = check_splited_configs(pyg, 3)\n",
    "print(ok)\n",
    "print(msg)\n",
    "print(configs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'ok': False, 'errmsg': 'NoScannerAvailable'}\n"
     ]
    }
   ],
   "source": [
    "url = f\"http://127.0.0.1:9394/gvm/create_splite_task\"\n",
    "resp = requests.post(\n",
    "        url,\n",
    "                    headers={'secret-key':\"xxxxx\"},\n",
    "                    params={'target':'192.168.220.132', 'splite_num':2}\n",
    "                )\n",
    "resp.raise_for_status()\n",
    "data = resp.json()\n",
    "print(data)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
